limsPurchase.editPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-purchase-edit-list-grid',
            url: limsPurchase.baseUrl + '/queries/raw',
            service: 'limsPurchaseServiceImpl',
            dbTable: 'T_LIMS_PURCHASE',
            columns: limsPurchase.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    // _this.refreshTabs(null)
                }
            },
            onRowActive: function (index, row) {
                // _this.refreshTabs(row)
            },
            deleteFormatter: function (row) {
                return row.applyCode
            },
            onCellClick: function (field, row) {
                field === 'applyCode' && _this.loadDetailPage(row)
            },
            page: true,
            requestData: {
                createdById_EQ: workspace.user.id,
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.BASEDATA.PURCHASES.MODAL.ADD',
                        url: limsPurchase.baseUrl,
                        fields: limsPurchase.getBaseInfoFormFields(),
                    }).done(function (id) {
                        Gikam.getComp('lims-purchase-edit-list-grid').refresh();
                        _this.loadDetailPage({id: id})
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-purchase-edit-list-grid').deleteRows(limsPurchase.baseUrl);
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    var grid = Gikam.getComp('lims-purchase-edit-list-grid');
                    var selection = grid.getSelections();
                    if (selection.length > 1 || Gikam.isEmpty(selection)) {
                        Gikam.alert('请一次选择一个单子提交！')
                        return
                    } else {
                        Gikam.create('workflow').submit({
                            data: grid.getSelections(),
                            pageObject: limsPurchase,
                            formatterToolbar: (toolbar, typsetCompConfige, formId) => {
                                toolbar.forEach(b => {
                                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                        b.class = 'blue'
                                        b.icon = 'save'
                                        b.color = 'white'
                                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                        b.icon = 'back'
                                    }
                                })
                                return toolbar
                            }
                        }).done(function () {
                            grid.refresh();
                        });
                    }
                }
            }]
        }
    },

    loadDetailPage: function (row) {
        workspace.window.load(limsPurchase.baseUrl + '/page/detail' + Gikam.param({
            id: row.id,
            step: 'edit'
        }));
    },

    refreshTabs: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.auditRecordGrid).done(function (comp) {
            if (row && row.id) {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_PURCHASE&id=' + row.id
                })
            } else {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_PURCHASE&id=' + 0
                })
            }
        })
        Gikam.getAsyncComp('lims-purchase-edit-list-material-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials/queries', {
                        id: row.id
                    })
                });
            } else {
                grid.refresh({
                    url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials/queries', {
                        id: -1
                    })
                });
            }
        });
        _this.refreshAttachments(_this.idManager.trainRecordGrid)
    },

    getMaterialGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-purchase-edit-list-material-grid',
            service: 'limsPurchaseMaterialServiceImpl',
            dbTable: 'T_LIMS_PURCHASE_MATERIAL',
            columns: limsPurchase.getMaterialListColumns(),
            deleteFormatter: function (row) {
                return '第' + (row.index + 1) + '行'
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add-row',
                onClick: function () {
                    var activedRow = Gikam.getComp('lims-purchase-edit-list-grid').getActivedRow()
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                        return false
                    }
                    Gikam.getComp('lims-purchase-edit-list-material-grid').insert(Gikam.printf(limsPurchase.baseUrl + '/{id}/materials', {
                        id: Gikam.getComp('lims-purchase-edit-list-grid').getActivedRow().id
                    }), [{}]);
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-purchase-edit-list-material-grid').deleteRows(Gikam.printf(limsPurchase.baseUrl + '/{id}/materials', {
                        id: Gikam.getComp('lims-purchase-edit-list-grid').getActivedRow().id
                    }));
                }
            }],

        };
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId)
        let actRow = grid.getActivedRow()
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable, readonly) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            genericQuery: false,
            readonly: readonly,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    getAuditRecordGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.auditRecordGrid,
            columns: limsPurchase.getFlowListColumns()
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-purchase-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getGridParam()]
            }
        });
    },

    idManager: {
        trainRecordGrid: 'lims-purchase-edit-list-grid',
        auditRecordGrid: 'lims-purchase-edit-list-grid' + '-record',
    },

    init: function () {
        this.create();
    }
};
